package com.example.workflow.service.impl;

import com.example.workflow.dto.ApprovalHistoryListDto;
import com.example.workflow.dto.ProcessedApprovalListDto;
import com.example.workflow.dto.ApprovalHistoryListDto.ApprovalHistoryItem;
import com.example.workflow.mapper.ApprovalOrderMapper;
import com.example.workflow.service.ApprovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 审批服务实现类
 */
@Service
@Slf4j
public class ApprovalServiceImpl implements ApprovalService {
    
    @Autowired
    private ApprovalOrderMapper approvalOrderMapper;
    
    /**
     * 获取已处理审批工单统计数据
     * URL: /api/approval/processed
     * 
     * @param userId 用户ID
     * @param role 用户角色
     * @return 已处理审批工单统计数据
     */
    @Override
    public ProcessedApprovalListDto getProcessedApprovalOrders(
            Integer userId, 
            String role) {
        
        int totalCount = 0;
        int approvedCount = 0;
        int rejectedCount = 0;
        
        log.info("获取已处理审批工单统计数据，用户ID: {}, 角色: {}", userId, role);
        
        // 根据角色查询不同的审批工单统计数据
        if ("CITY".equals(role)) {
            log.info("执行一级审批已处理工单统计");
            
            // 统计工单数量
            totalCount = approvalOrderMapper.countProcessedFirstApprovalOrders(userId);
                
            // 统计通过和拒绝的工单数量
            approvedCount = approvalOrderMapper.countApprovedFirstOrders(userId);
            rejectedCount = approvalOrderMapper.countRejectedFirstOrders(userId);
                
            log.info("一级审批已处理工单统计结果: 总数 = {}, 通过 = {}, 拒绝 = {}", 
                    totalCount, approvedCount, rejectedCount);
                    
        } else if ("PROVINCE".equals(role)) {
            log.info("执行二级审批已处理工单统计");
            
            // 统计工单数量
            totalCount = approvalOrderMapper.countProcessedSecondApprovalOrders(userId);
                
            // 统计通过和拒绝的工单数量
            approvedCount = approvalOrderMapper.countApprovedSecondOrders(userId);
            rejectedCount = approvalOrderMapper.countRejectedSecondOrders(userId);
                
            log.info("二级审批已处理工单统计结果: 总数 = {}, 通过 = {}, 拒绝 = {}", 
                    totalCount, approvedCount, rejectedCount);
                    
        } else {
            log.warn("未知角色: {}, 无法查询已处理工单", role);
            return new ProcessedApprovalListDto(0, 0, 0);
        }
        
        // 封装并返回结果
        ProcessedApprovalListDto result = new ProcessedApprovalListDto();
        result.setTotalCount(totalCount);
        result.setApprovedCount(approvedCount);
        result.setRejectedCount(rejectedCount);
        return result;
    }
    
    /**
     * 获取审批人员的历史审批工单列表
     * URL: /approval/list
     * 
     * @param userId 用户ID
     * @param role 用户角色
     * @return 审批历史工单列表
     */
    @Override
    public ApprovalHistoryListDto getApprovalHistoryList(Integer userId, String role) {
        List<ApprovalHistoryItem> historyItems = new ArrayList<>();
        
        log.info("获取审批历史工单列表，用户ID: {}, 角色: {}", userId, role);
        
        // 根据角色查询不同的审批工单列表
        if ("CITY".equals(role)) {
            log.info("执行一级审批历史工单查询");
            historyItems = approvalOrderMapper.getFirstApprovalHistoryList(userId);
            log.info("查询到一级审批历史工单 {} 条", historyItems.size());
        } else if ("PROVINCE".equals(role)) {
            log.info("执行二级审批历史工单查询");
            historyItems = approvalOrderMapper.getSecondApprovalHistoryList(userId);
            log.info("查询到二级审批历史工单 {} 条", historyItems.size());
        } else {
            log.warn("未知角色: {}, 无法查询历史审批工单", role);
        }
        
        // 封装返回结果
        ApprovalHistoryListDto result = new ApprovalHistoryListDto();
        result.setItems(historyItems);
        result.setTotalCount(historyItems.size());
        
        return result;
    }
} 